/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.atomojo.tools.media;

import java.io.Console;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.atomojo.tools.media.sync.DirectorySynchronizer;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.data.Protocol;
import org.restlet.data.Reference;

/**
 *
 * @author alex
 */
public class MediaTool {

   static Logger LOG = Logger.getLogger(MediaTool.class.getName()); 
   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
      if (args.length<3) {
         System.err.println("Usage: "+MediaTool.class.getName()+" ( upload | download | both ) ( username{:password} | - ) service-uri dir1 ...");
         System.exit(1);
      }
      
      Context context = new Context();
      context.setLogger(LOG);
      
      List<Protocol> clientProtocols = new ArrayList<Protocol>();
      clientProtocols.add(Protocol.HTTP);
      clientProtocols.add(Protocol.HTTPS);
      Client client = new Client(clientProtocols);
      client.setContext(context);
      
      context.setClientDispatcher(client);

      DirectorySynchronizer.Mode mode = DirectorySynchronizer.Mode.Upload;
      if (args[0].equals("upload")) {
         mode = DirectorySynchronizer.Mode.Upload;
      } else if (args[0].equals("download")) {
         mode = DirectorySynchronizer.Mode.Download;
      } else if (args[0].equals("both")) {
         mode = DirectorySynchronizer.Mode.UploadAndDownload;
      } else {
         System.err.println("Unrecognized mode: "+args[0]);
         System.exit(1);
      }
      MediaBrowser browser = new MediaBrowser(context);
      Console console = System.console();
      if (args[1].equals("-")) {
         String username = console.readLine("Username:");
         char [] password = console.readPassword("Password:");
         browser.setIdentity(username,new String(password));
      } else {
         int colon = args[1].indexOf(':');
         if (colon<0) {
            char [] password = console.readPassword("Password:");
            browser.setIdentity(args[1],new String(password));
         } else {
            browser.setIdentity(args[1].substring(0,colon),args[1].substring(colon+1));
         }
      }
      DirectorySynchronizer dirSync = new DirectorySynchronizer(browser);
      dirSync.setMode(mode);
      Reference service = new Reference(args[2]);
      for (int i=2; i<args.length; i++) {
         File dir = new File(args[i]);
         boolean newDir = false;
         if (mode!=DirectorySynchronizer.Mode.Upload && !dir.exists()) {
            if (dir.mkdir()) {
               newDir = true;
            } else {
               LOG.severe("Cannot create directory "+dir.getAbsolutePath());
               continue;
            }
         }
         if (dir.exists()) {
            if (dir.isDirectory()) {
               dirSync.sync(dir,newDir,service);
            } else {
               LOG.warning(dir.getAbsolutePath()+" is not a directory.");
            }
         } else {
            LOG.warning(dir.getAbsolutePath()+" does not exist.");
         }
      }
      
   }
}
